package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.my.adapter.FansListAdapter;
import com.cn.kabalifeuser.ui.my.bean.FansBean;
import com.cn.kabalifeuser.ui.my.bean.FansListBean;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.NotDataView;
import com.cn.kabalifeuser.view.dialogView.InviteesDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/4.
 * function:
 */
public class FansListAcivity extends IBaseActivity<FansListView, FansListPresenter> implements FansListView {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    List<FansListBean.ResultBean.DataBean> mList;
    FansListAdapter fansListAdapter;
    @BindView(R.id.tv_fans_num)
    TextView tvFansNum;
    @BindView(R.id.tv_today)
    TextView tvToday;
    @BindView(R.id.tv_yesterday)
    TextView tvYesterday;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private View notDataView;


    int order = 1;
    int num = 1;
    FansBean fansBean;
    InviteesDialog inviteesDialog;

    @Override
    protected FansListPresenter createPresenter() {
        return new FansListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_fans;
    }


    @OnClick(R.id.ll_btn_set)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        rvData.setLayoutManager(layoutManager3);
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        fansListAdapter = new FansListAdapter(R.layout.activity_fans_item, mList);
        notDataView = NotDataView.notDataView(this, rvData, refreshLayout);
        rvData.setAdapter(fansListAdapter);
        initViewData(1);
        mPresenter.onFansData(LoginHelper.getLonginData().getResult());

        pagehttp = 1;
        getListData();


    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onFansListData(LoginHelper.getLonginData().getResult(), order, num, pagehttp, pageNum);
    }

    @OnClick({R.id.ll_btn_time, R.id.ll_btn_num, R.id.ll_btn_see})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_time:
                initViewData(1);
                pagehttp = 1;
                getListData();
                break;
            case R.id.ll_btn_num:
                initViewData(2);
                pagehttp = 1;
                getListData();
                break;
            case R.id.ll_btn_see:
                if (fansBean != null)
                    if (fansBean.getResult() != null && fansBean.getResult().getSuperior() != null) {
                        if (inviteesDialog == null)
                            inviteesDialog = new InviteesDialog(getMe());
                        inviteesDialog.getTv_name().setText("昵称: " + fansBean.getResult().getSuperior().getNickname());
                        inviteesDialog.getTv_phone().setText("手机号码: " + fansBean.getResult().getSuperior().getPhone());
                        GlideUtil.ImageLoad(getMe(), fansBean.getResult().getSuperior().getHead(), inviteesDialog.getImg_head());
                        inviteesDialog.getTv_btn_next().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                inviteesDialog.dismiss();
                            }
                        });
                        inviteesDialog.show();
                    }


                break;
        }
    }

    private void initViewData(int index) {
        switch (index) {
            case 1:
                tvTime.setTextColor(getResources().getColor(R.color.main_color));
                tvNum.setTextColor(getResources().getColor(R.color.text_666666));
                order = 1;
                break;
            case 2:
                tvTime.setTextColor(getResources().getColor(R.color.text_666666));
                tvNum.setTextColor(getResources().getColor(R.color.main_color));
                order = 2;
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {

        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);
    }


    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onFansSuccess(FansBean bean) {

        fansBean = bean;
        tvFansNum.setText(String.valueOf(bean.getResult().getFans()));
        tvToday.setText(String.valueOf(bean.getResult().getToday()));
        tvYesterday.setText(String.valueOf(bean.getResult().getYesterday()));
    }

    @Override
    public void onFansListSuccess(FansListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                fansListAdapter.replaceData(bean.getResult().getData());
            } else {
                fansListAdapter.replaceData(bean.getResult().getData());
                fansListAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                fansListAdapter.addData(bean.getResult().getData());
            }

        }
    }


}
